package com.marcinklos.filipides;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.marcinklos.filipides.web.PMF;

public class UserRegistry {

	public User findUser(String userId) {
		User user = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(User.class);
		query.setFilter("googleUserId == '" + userId +"'");
		List<User> users = (List<User>)query.execute();
		
		System.out.println("Nonexistent user id [" + userId + "]" + users.isEmpty());
		
		if (!users.isEmpty()) user = users.get(0);
		pm.close();
		return user;
	}
	
	public void registerUser(User user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(user);
		pm.close();
	}

}
